//
//  AttendanceViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/4/12.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class AttendanceViewController: BaseViewController,UITableViewDataSource,UITableViewDelegate {
    
    var tableView: UITableView!
    
    var array = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.title = "今日考勤"
        
        InitwViews()
        
    }
    func InitwViews()  {
        tableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: screenH))
        tableView.backgroundColor = UIColor.init(hexString: "f6f6f6", alpha: 1)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
        self.view.addSubview(tableView)
        setUpRefresh()
    }
    func setUpRefresh() {
        tableView.header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(LeaveViewController.refresh))
        tableView.header.beginRefreshing()
    }
    func refresh() {
        
        if (reachability?.isReachable())! {
            
            let user = getUserPojo()
            let request = requestHeader + "18</t><sch>" + user.schid + "</sch></channel></rss>"
            let url = server + adminwebapppi
            
            array = getData(url, request: request)
            tableView.reloadData()
            
        } else {
            HYLog("网络连接：不可用")
        }
        
        tableView.header.endRefreshing()
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "CourseTableViewCellIdentifier";
        
        let cell: CourseTableViewCell = CourseTableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: cellIdentifier)
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        let dic = array[indexPath.row] as! JSON
        
        let isatten = dic["isatten"].stringValue
        let classname = dic["classname"].stringValue
        
        cell.classname.text = classname
        if isatten == "0" {
            cell.read.image = UIImage.init(named: "laba")
        }else {
            cell.read.image = UIImage.init(named: "laba2")
        }
        
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let dic = array[indexPath.row] as! JSON
        let classname = dic["classname"].stringValue
        let isatten = dic["isatten"].stringValue
        
        if isatten == "0" {
            TSMessage.showNotification(in: self, title: "提示", subtitle: "\(classname),班主任老师今日尚未添加考勤数据！", image: nil, type: TSMessageNotificationType.error, duration: 2.0, callback: nil, buttonTitle: "知道了", buttonCallback: nil, at: TSMessageNotificationPosition.top, canBeDismissedByUser: true)
        }else{
            let dic = array[indexPath.row] as! JSON
            let classid = dic["classid"].stringValue
            let attendance = AttendanceContentViewController()
            attendance.classid = classid
            self.navigationController?.pushViewController(attendance, animated: true)
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
